#include "ephysics.h"
#include <Box2D/Box2D.h>

EPhysics::EPhysics(const QPointF &gravity)
{
    m_pWorld = new b2World(b2Vec2(gravity.x(), gravity.y()));
}

EPhysics::~EPhysics()
{
    delete m_pWorld;
}

void EPhysics::update(float dt)
{
    float timeStep = 1.0f/60.0f;
    int velocityIterations = 10;
    int positionIterations = 6;
    m_pWorld->Step(timeStep, velocityIterations, positionIterations);
}

b2World *EPhysics::world() const
{
    return m_pWorld;
}

b2Body *EPhysics::createBody(EPhysics::BodyType type, float initialX, float initialY, float initialAngleDeg)
{
    b2BodyDef def;

    switch(type)
    {
    case BT_Static:
        def.type = b2_staticBody;
        break;
    case BT_Kinematic:
        def.type = b2_kinematicBody;
        break;
    case BT_Dynamic:
        def.type = b2_dynamicBody;
        break;
    }

    def.position = b2Vec2(initialX, initialY);
    def.angle = initialAngleDeg*3.1415f/180.0f;

    return m_pWorld->CreateBody(&def);
}
